﻿using System.Collections.Generic;
using System.Modules.Contracts.DTO;
using System.Text;

namespace System.Modules.SharePoint.Contracts.DTO
{
    /// <summary>
    /// SP Field Collection
    /// </summary>
    public class SPQuerFieldsCollection : Initiator, IList<SPQueryFieldRef>
    {
        #region Constructor
        /// <summary>
        /// Default Constructor
        /// </summary>
        public SPQuerFieldsCollection() {}

        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="items"></param>
        public SPQuerFieldsCollection(List<SPQueryFieldRef> items)
        {
            Items = items;
        } 
        #endregion

        #region Properties
        /// <summary>
        /// Fields
        /// </summary>
        public List<SPQueryFieldRef> Items { get; set; }
        #endregion

        #region Implicit Operators
        /// <summary>
        /// Implicit conversion operator to string
        /// </summary>
        /// <param name="value">The SP Query Value</param>
        /// <returns>A string representation of the SP Query Field Ref object</returns>
        public static implicit operator string(SPQuerFieldsCollection value)
        {
            return value.ToString();
        }
        #endregion

        #region Override
        /// <summary>
        /// Overrides To String
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            var value = new StringBuilder();
            foreach (var field in Items)
            {
                value.Append(field);
            }
            return value.ToString();
        }
        #endregion

        #region Methods
        public int IndexOf(SPQueryFieldRef item)
        {
            return Items.IndexOf(item);
        }

        public void Insert(int index, SPQueryFieldRef item)
        {
            Items.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            Items.RemoveAt(index);
        }

        public SPQueryFieldRef this[int index]
        {
            get
            {
                if (Items.Count > index)
                {
                    return Items[index];
                }
                throw new IndexOutOfRangeException();
            }
            set
            {
                if (Items.Count > index)
                {
                    Items[index] = value;
                }
                else
                {
                    Items.Add(value);
                }
            }
        }

        public void Add(SPQueryFieldRef item)
        {
            Items.Add(item);
        }

        public void Clear()
        {
            Items.Clear();
        }

        public bool Contains(SPQueryFieldRef item)
        {
            return Items.Contains(item);
        }

        /// <summary>
        /// Contains
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public bool Contains(string fieldName)
        {
            return Items.Exists(match => match.FieldName.Equals(fieldName, StringComparison.CurrentCultureIgnoreCase));
        }

        public void CopyTo(SPQueryFieldRef[] array, int arrayIndex)
        {
            Items.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return Items.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(SPQueryFieldRef item)
        {
            return Items.Remove(item);
        }

        /// <summary>
        /// Remove
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public bool Remove(string fieldName)
        {
            return Items.RemoveAll(item => item.FieldName.Equals(fieldName, StringComparison.CurrentCultureIgnoreCase)) > 0;
        }

        public IEnumerator<SPQueryFieldRef> GetEnumerator()
        {
            return Items.GetEnumerator();
        }

        Collections.IEnumerator Collections.IEnumerable.GetEnumerator()
        {
            return Items.GetEnumerator();
        }
        #endregion
    }
}
